package com.gxkj.projects.myshopx.dao;

import com.gxkj.common.hibernate.HibernateBaseRepositoryImpl;
import com.gxkj.common.utils.ListPager;
import com.gxkj.projects.myshopx.entitys.Attribute;
import com.gxkj.projects.myshopx.entitys.Brand;
import com.gxkj.projects.myshopx.entitys.User;
import com.gxkj.projects.myshopx.enums.AttributeStates;
import com.gxkj.projects.myshopx.enums.BrandStates;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class BrandDaoImpl  extends HibernateBaseRepositoryImpl {

    public ListPager<Brand> doPageHQL(int pagenNo, int pageSize, Brand brand){
        ListPager<Brand> pager = new ListPager<Brand>();
        pager.setPageNo(pagenNo);
        pager.setRowsPerPage(pageSize);
        String hql  = " from Brand b   ";
        Map<String,Object> param = new HashMap<String,Object>();
        if(brand==null || brand.getStates() == null   ){
            hql += " where states != :states ";
            param.put("states", BrandStates.DEL);

        }else {
            hql += " where states = :states ";
            param.put("states",brand.getStates());
        }
        if(brand!=null){
            if(StringUtils.isNotBlank(brand.getBrandName())){
                hql += " and brandName like :brandName ";
                param.put("brandName","%"+brand.getBrandName()+"%");
            }

        }

        return  this.selectPageByHql(hql,param,pager);
    }

    public boolean  isExitByName(String id,String name){

        String hql = null;
        Map<String,Object> param = new HashMap<String,Object>();
        if(StringUtils.isBlank(id)){
            hql = " from Brand a where states != :states and brandName=:name  ";
            param.put("states", AttributeStates.DEL);
            param.put("name",name);
        }else {
            hql = " from Brand a where states != :states and brandName=:name and id !=:id   ";
            param.put("states", AttributeStates.DEL);
            param.put("name",name);
            param.put("id",id);
        }
        Brand entity = this.selectFirstOneByHQL(hql,param);
        if(entity == null){
            return false;
        }else {
            return true;
        }
    }
}
